package com.uenpay.im.ui.old.activity.dynamic;

import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * dynamic msg history
 * 
 * @author SL
 * @copyRighgt http://www.qiyunxin.com
 * 
 */
public class GetDynamicsHistoryInvokeItem extends BaseHttpInvokeItem {
	public class GetDynamicsHistoryInvokeItemResult extends HttpInvokeResult {
		public ArrayList<Reply> replys = new ArrayList<Reply>();
	}

	public GetDynamicsHistoryInvokeItemResult getOutPut() {
		return (GetDynamicsHistoryInvokeItemResult) GetResultObject();
	}

	public GetDynamicsHistoryInvokeItem(int page) {
		String url = APIConfiguration.getBaseUrl()
				+ "Dynamic/Dynamic/getRelationList?pageIndex=" + page + "&pageSize=12&" + APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetDynamicsHistoryInvokeItemResult result = new GetDynamicsHistoryInvokeItemResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		JSONArray array = json.optJSONArray("data");
		if (array != null) {
			for(int i =0,size = array.length(); i< size; i++){
				result.replys.add(deserializeReply(array.optJSONObject(i)));
			}
		}

		return result;
	}
	
	private Reply deserializeReply(JSONObject json){
		Reply reply = new Reply();
		reply.replyid = json.optString("replyid");
		reply.dynamicid = json.optString("dynamicid");
		reply.custid = json.optString("custid");
		reply.pcustid = json.optInt("pcustid");
		reply.content = json.optString("content");
		reply.nickname = json.optString("nickname");
		reply.type = json.optInt("type");
		reply.createtime = json.optString("createtime");
		reply.d_content = json.optString("d_content");
		JSONObject fileObject = json.optJSONObject("files");
		if(fileObject != null){
			reply.fileMessage.fileid = fileObject.optString("fileid");
			reply.fileMessage.filehash = fileObject.optString("filehash");
		}
		return reply;
	}
}
